package com.gg.web;

import com.gg.pojo.Book;
import com.gg.pojo.Page;
import com.gg.service.BookService;
import com.gg.service.impl.BookServiceImpl;
import com.gg.utils.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class ClientBookServlet extends BaseServlet{
    private BookService bookService = new BookServiceImpl();

    protected void page(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("经过了前台的ClientBookServlet程序");

        //1.获取请求参数pageNO 和 pageSize 转为int型
        int pageNo = WebUtils.parserInt(req.getParameter("pageNo"),1);
        int pageSize = WebUtils.parserInt(req.getParameter("pageSize"), Page.PAGE_SIZE);
        //2.调用BookService.page(pageNo,pageSize):Page对象
        Page<Book> page = bookService.page(pageNo, pageSize);
            //设置前台分页方法的地址
            page.setUrl("client/bookServlet?action=page");
        //3.保存Page对象到Request域中,也就是Page对象的五个属性
        req.setAttribute("page",page);
        //4.请求转发到pages/manager/book_manager.jsp页面
        req.getRequestDispatcher("/pages/client/index.jsp").forward(req,resp);
    }

    protected void pageByPrice(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.获取请求参数pageNo,pageSize,min,max
        int pageNo = WebUtils.parserInt(req.getParameter("pageNo"),1);
        int pageSize = WebUtils.parserInt(req.getParameter("pageSize"), Page.PAGE_SIZE);
        int min = WebUtils.parserInt(req.getParameter("min"),0);
        int max = WebUtils.parserInt(req.getParameter("max"), Integer.MAX_VALUE);
        //2.调用bookService.pageByPrice():Page对象
        Page<Book> page = bookService.pageByPrice(pageNo, pageSize,min,max);
            //设置page的url
            page.setUrl("client/bookServlet?action=pageByPrice" + "&min="+req.getParameter("min")+"" + "&max="+req.getParameter("max")+"");
        //3.保存page对象到Request域中
        req.setAttribute("page",page);
        //4.请求转发到/pages/client/index.jsp
        req.getRequestDispatcher("/pages/client/index.jsp").forward(req,resp);
    }
}
